package com.dji.sample.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dji.sample.manage.dao.DevicePositionMapper;
import com.dji.sample.manage.model.entity.DevicePositionEntity;
import com.dji.sample.manage.model.receiver.DevicePositionReceiver;
import com.dji.sample.manage.service.DevicePositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author yjt
 * @date 2022-09-09
 * @description:
 */
@Service
public class DevicePositionServiceImpl implements DevicePositionService {

    @Autowired
    DevicePositionMapper devicePositionMapper;

    @Override
    public void save(DevicePositionReceiver devicePositionReceiver) {
        DevicePositionEntity entity = DevicePositionEntity.builder().deviceSn(devicePositionReceiver.getDeviceSn())
                .latitude(devicePositionReceiver.positionItemReceiver.latitude)
                .createTime(new Date())
                .longitude(devicePositionReceiver.positionItemReceiver.longitude)
                .deviceTimestamp(devicePositionReceiver.getTimestamp())
                .height(devicePositionReceiver.positionItemReceiver.height)
                .data(devicePositionReceiver.getData()).build();
        devicePositionMapper.insert(entity);
    }

    @Override
    public List<DevicePositionEntity> selectDevicePosition(String deviceSn, String time) {
        return devicePositionMapper.selectDevicePosition(deviceSn, time);
    }

    @Override
    public DevicePositionEntity getLastDevicePosition(String deviceSn, String time) {
        return devicePositionMapper.selectOne(new LambdaQueryWrapper<DevicePositionEntity>().eq(DevicePositionEntity::getDeviceSn, deviceSn).gt(DevicePositionEntity::getCreateTime, time).orderByDesc(DevicePositionEntity::getId).last(" limit 1"));
    }

    @Override
    public List<DevicePositionEntity> list(String deviceSn, Long geId, Long leId) {
        return devicePositionMapper.selectList(new LambdaQueryWrapper<DevicePositionEntity>().select(DevicePositionEntity::getId,DevicePositionEntity::getLatitude,DevicePositionEntity::getLongitude,DevicePositionEntity::getHeight,DevicePositionEntity::getCreateTime).eq(DevicePositionEntity::getDeviceSn,deviceSn).ge(DevicePositionEntity::getId,geId).le(DevicePositionEntity::getId,leId));
    }
}
